const fs = require("fs")
const {
	update
} = require("../control/user")
const md5 = require("md5")
const {
	updateProd,
	addProd
} = require("../control/prod")
const { addPDF } = require("../control/PDF")
const {addMusic} = require("../control/music")
const {
	SuccessModel,
	ErrorModel
} = require("../model/resModel")


const upload = async function (req, res) {
	//重命名 直接上传的文件名字为随机名字 
	let {
		username
	} = req.body
	let oldPath = req.file.destination + '/' + req.file.filename
	let newPath = req.file.destination + '/' + req.file.filename + req.file.originalname
	let usePath = "public/images/" + req.file.filename + req.file.originalname
	fs.rename(oldPath, newPath, () => {
		console.log(newPath)
	})
	req.body.avatar = "http://localhost:3001/api/upload/showImg?avatar=" + usePath + "&username=" + username
	let data1 = await update(req, res)
	if (data1.affectedRows > 0) {
		res.send(new SuccessModel(req.body.avatar, "上传成功"))
	} else {
		res.send(new ErrorModel("上传失败"))
	}
}

const uploadMp4 = async function (req, res) {
	//重命名 直接上传的文件名字为随机名字 
	let {
		username,
		origin,
		text
	} = req.body

	req.files.forEach(element => {

		if (element.mimetype.split("/")[0] == "image") {
			let oldPath = element.destination + '/' + element.filename
			let newPath = (element.destination + '/' + element.filename + element.originalname).replace(/[\u4e00-\u9fa5]/g, '')
			let usePath = "public/Mp4/" + (element.filename + element.originalname).replace(/[\u4e00-\u9fa5]/g, '')
			fs.rename(oldPath, newPath, () => {
				console.log(newPath)
			})
			req.body.poster = "http://localhost:3001/api/upload/showMp4?poster=" + usePath + "&username=" + username
		} else {
			let oldPath = element.destination + '/' + element.filename
			let newPath = (element.destination + '/' + element.filename + element.originalname).replace(/[\u4e00-\u9fa5]/g, '')
			let usePath = "public/Mp4/" + (element.filename + element.originalname).replace(/[\u4e00-\u9fa5]/g, '')
			fs.rename(oldPath, newPath, () => {
				console.log(newPath)
			})
			req.body.prod = "http://localhost:3001/api/upload/showMp4?prod=" + usePath + "&username=" + username

		}
	});



	let data1
	if (origin == "update") {
		data1 = await updateProd(req, res)

	} else if (origin == "add") {
		data1 = await addProd(req, res)
	}
	if (data1.affectedRows > 0) {

		res.send(new SuccessModel(req.body.avatar, "上传成功"))
	} else {
		res.send(new ErrorModel("上传失败"))
	}
}


const uploadMusic = async function (req, res) {
	//重命名 直接上传的文件名字为随机名字 
	let {
	} = req.body
	
	let ifSendSuccess = true
	req.files.forEach(async element => {
		let currentTime = new Date().getTime()
		let lastName = element.originalname.split('.')
		let lockName = md5(element.filename + currentTime)+'.'+ lastName[lastName.length-1]
		let oldPath = element.destination + '/' + element.filename
		let newPath = (element.destination + '/' + lockName).replace(/[\u4e00-\u9fa5]/g, '')
		let usePath = "public/music/" + lockName.replace(/[\u4e00-\u9fa5]/g, '')
		fs.rename(oldPath, newPath, () => {
			console.log(newPath)
		})
		req.body.music = "http://localhost:3001/api/upload/music?music=" + usePath 
		let data1 = await addMusic(req, res)
		console.log(data1.affectedRows);
		
		if (data1.affectedRows === 0) {
			ifSendSuccess = false
		}
	});
	ifSendSuccess ?
		res.send(new SuccessModel("上传成功"))
		:
		res.send(new ErrorModel("上传失败"))





}

const uploadPDF = async function (req, res) {
	//重命名 直接上传的文件名字为随机名字 
	let {
		username,
	} = req.body
	let ifSendSuccess = true
	req.files.forEach(async element => {
		let currentTime = new Date().getTime()
		let lastName = element.originalname.split('.')
		let lockName = md5(element.filename + currentTime)+'.'+ lastName[lastName.length-1]
		let oldPath = element.destination + '/' + element.filename
		let newPath = (element.destination + '/' + lockName).replace(/[\u4e00-\u9fa5]/g, '')
		let usePath = "public/PDF/" + lockName.replace(/[\u4e00-\u9fa5]/g, '')
		fs.rename(oldPath, newPath, () => {
			console.log(newPath)
		})
		req.body.pdf = "http://localhost:3001/api/upload/PDF?pdf=" + usePath + "&username=" + username
		req.body.size = element.size * 1 / 1024 / 1024 + "M"
		req.body.name = element.originalname
		let data1 = await addPDF(req, res)
		if (data1.affectedRows === 0) {
			ifSendSuccess = false
		}
	});
	ifSendSuccess ?
		res.send(new SuccessModel("上传成功"))
		:
		res.send(new ErrorModel("上传失败"))





}

module.exports = {
	upload,
	uploadMp4,
	uploadPDF,
	uploadMusic
}